/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.management.user.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tompai.common.dao.impl.SysUserMapper;
import com.tompai.common.enums.EnableStatusEnums;
import com.tompai.common.enums.OnlineStatusEnums;
import com.tompai.common.model.SysUser;
import com.tompai.common.service.impl.BaseServiceImpl;
import com.tompai.management.user.service.SysUserService;
import com.tompai.management.user.web.vo.SysUserVo;

/**
 * 用户service层接口
 * 
 * @author liinux@qq.com
 * @date 2019/10/6
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {

	@Autowired
	private SysUserMapper sysUserMapper;

	@Override
	public SysUser getSysUserInfo(String id) {
		return sysUserMapper.selectByPrimaryKey(id);
	}

	@Override
	public PageInfo<SysUserVo> selectSysUserInfo(SysUser sysUser, int page, int limit) {
		PageHelper.startPage(page, limit);
		List<SysUser> select = sysUserMapper.select(sysUser);
		List<SysUserVo> sysUserVos = new ArrayList<>();
		for (SysUser sysUser1 : select) {
			SysUserVo sysUserVo = new SysUserVo();
			sysUserVo.setId(sysUser1.getId());
			sysUserVo.setAccount(sysUser1.getAccount());
			EnableStatusEnums enableStatusEnums = EnableStatusEnums.ofValue(sysUser1.getEnable());
			sysUserVo.setEnable(enableStatusEnums == null ? "" : enableStatusEnums.getName());
			sysUserVo.setLastLoginIpAddress(sysUser1.getLastLoginIpAddress());
			sysUserVo.setLastLoginTime(sysUser1.getLastLoginTime());
			sysUserVo.setLoginCount(sysUser1.getLoginCount());
			sysUserVo.setName(sysUser1.getName());
			sysUserVo.setOrgId(sysUser1.getOrgId());
			OnlineStatusEnums onlineStatusEnums = OnlineStatusEnums.ofValue(sysUser1.getStatus());
			sysUserVo.setStatus(onlineStatusEnums == null ? "" : onlineStatusEnums.getName());
			sysUserVo.setPassword(sysUser1.getPassword());
			sysUserVo.setRemark(sysUser1.getRemark());
			sysUserVos.add(sysUserVo);
		}
		return new PageInfo<>(sysUserVos);
	}

	@Override
	public void update(SysUser sysUser) {
		sysUserMapper.updateByPrimaryKey(sysUser);
	}

	@Override
	public SysUser select(SysUser sysUser) {
		return sysUserMapper.selectOne(sysUser);
	}
}
